package cn.kimoon.dau_android;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * Created by xiangdong on 2016/11/22.
 */

public class TabRootActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private TextView title_text_view;

    private RadioGroup tab_bar_rg;
    private RadioButton engine_room_rb;

    // Fragment Object
    private EngineRoomFragment fg1;
    private MineFragment fg2;
    private FragmentManager fManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_root);

        initActionBar();
        initSubViews();
    }

    private void initActionBar() {
//        ActionBar actionBar = getSupportActionBar();
//        actionBar.hide();
    }

    private void initSubViews() {
        //Title
        title_text_view = (TextView) findViewById(R.id.title_text_view);

        //Fragment
        fManager = getFragmentManager();

        tab_bar_rg = (RadioGroup) findViewById(R.id.tab_bar_rg);
        tab_bar_rg.setOnCheckedChangeListener(this);
        // 获取第一个按钮，并使其置为选择状态
        engine_room_rb = (RadioButton) findViewById(R.id.engine_rome_rb);
        engine_room_rb.setChecked(true);
    }

    //RadioGroup按键处理
    @Override
    public void onCheckedChanged(RadioGroup group, int checkId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkId) {
            case R.id.engine_rome_rb:
                title_text_view.setText(R.string.tab_root_engine_room);
                if (fg1 == null) {
                    fg1 = new EngineRoomFragment();
                    fTransaction.add(R.id.frame_layout, fg1);
                } else {
                    fTransaction.show(fg1);
                }
                break;
            case R.id.mine_rb:
                title_text_view.setText(R.string.tab_root_personal_center);
                if (fg2 == null) {
                    fg2 = new MineFragment();
                    fTransaction.add(R.id.frame_layout, fg2);
                } else {
                    fTransaction.show(fg2);
                }
                break;
        }
        fTransaction.commit();
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (fg1 != null) {
            fragmentTransaction.hide(fg1);
        }
        if (fg2 != null) {
            fragmentTransaction.hide(fg2);
        }
    }
}
